package org.csu.mypetstore.controller;


import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.CartItem;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.SessionScope;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;

@Controller
@SessionScope
@RequestMapping("/cart/")
@SessionAttributes({"cart","account"})
public class CartController {
    @Autowired
    private CatalogService catalogService;
    @Autowired
    private CartService cartService;

    @Autowired
    Cart cart;


    @GetMapping("viewCart")
    public String viewCart(@SessionAttribute("account") Account account, Model model){
        cart= cartService.getCart(account.getUsername());
        model.addAttribute("cart",cart);
        return "cart/Cart";
    }

    @GetMapping("addItemToCart")
    public String addItemToCart(String workingItemId,Account account,Cart cart,Model model){
        Item item = catalogService.getItem(workingItemId);
        if(cart.containsItemId(workingItemId)){
            cart.incrementQuantityByItemId(workingItemId);
            cartService.updateCart(account.getUsername(),item.getItemId(),item.getQuantity());
        }else{
            boolean isInStock = catalogService.isItemInStock(workingItemId);
            cart.addItem(item,isInStock);
            cartService.insertCart(account.getUsername(),workingItemId);
        }
        model.addAttribute("cart",cart);
        return "cart/Cart";
    }

    @GetMapping("removeItemFromCart")
    public String removeItemFromCart(String workingItemId, Account account,Cart cart,Model model){
        Item item = cart.removeItemById(workingItemId);
        model.addAttribute("cart",cart);
        cartService.removeCart(account.getUsername(),item.getItemId());
        if(item == null){
            model.addAttribute("msg", "Attempted to remove null CartItem from Cart.");
            return "common/error";
        }else{
            return "cart/Cart";
        }
    }
    @PostMapping("updateCartQuantities")
    public String updateCartQuantities(HttpServletRequest request,@SessionAttribute("account") Account account,Model model){
        Iterator<CartItem> cartItems = cart.getAllCartItems();
        while (cartItems.hasNext()){
            CartItem cartItem = cartItems.next();
            String itemId = cartItem.getItem().getItemId();
            try{
                int quantity = Integer.parseInt(request.getParameter(itemId));
                cart.setQuantityByItemId(itemId,quantity);
                System.out.println(quantity);
                System.out.println(itemId);
                System.out.println(account.getUsername());
                cartService.updateCart(account.getUsername(),itemId,quantity);
                if(quantity < 1){
                    cartItems.remove();
                }
            }catch (Exception e){

            }
        }
        model.addAttribute("cart",cart);
        return "cart/Cart";
    }





//    @PostMapping("updateCartQuantities")
//    public String updateCartQuantities(HttpServletRequest request,@SessionAttribute("account") Account account,@SessionAttribute("cart") Cart cart,Model model){
//        List<CartItem> cartItemList = cart.getCartItemList();
//
//        for(int i=0;i<cartItemList.size();i++){
//            int quantity = Integer.parseInt(request.getParameter(cartItemList.g);
//            System.out.println(quantity);
//            cart.setQuantityByItemId(cartItemList.get(i).getItemId(),quantity);
//            System.out.println(cartItemList.size());
//            if(cartItemList.get(i).getQuantity() < 1){
//                cart.removeItemById(cartItemList.get(i).getItemId());
//            }
//        }
//        model.addAttribute("cart",cart);
//        return "cart/Cart";
//
//    }

//    @PostMapping("updateCartQuantities")
//    public String updateCartQuantities(HttpServletRequest request,@SessionAttribute("account") Account account,Model model){
//
//        Iterator<CartItem> cartItems = cart.getAllCartItems();
//        while (cartItems.hasNext()){
//            CartItem cartItem = cartItems.next();
//            String itemId = cartItem.getItem().getItemId();
//            try{
//                int quantity = Integer.parseInt(request.getParameter(itemId));
//                cart.setQuantityByItemId(itemId,quantity);
//                cartService.updateCart(account.getUsername(),itemId,quantity);
//                if(quantity < 1){
//                    cartItems.remove();
//                }
//            }catch (Exception e){
//
//            }
//        }
//        model.addAttribute("cart",cart);
//        return "cart/Cart";
//    }
//









}
